package com.softjourn.punchcard.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.softjourn.punchcard.R;
import com.softjourn.punchcard.model.Offer;
import com.softjourn.punchcard.model.PunchCard;

public class OfferActivity extends SherlockActivity {
	
	Button btnBack;
	Button btnUseOffer;
	
	private Offer offer;
	
	private PunchCard punchCard;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_offer);
		
		getWindow().setSoftInputMode(
			      WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		btnBack = (Button) findViewById(R.id.btn_offer_back);
		btnUseOffer = (Button) findViewById(R.id.btn_use_offer);
		
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		btnUseOffer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent actQR = new Intent(OfferActivity.this, QRActivity.class);
				
				String messageQR = getString(R.string.identifier_offer) +
						offer.getUid() + getString(R.string.uid_delimeter) +
						punchCard.getUid();
				
				actQR.putExtra(QRActivity.class.getCanonicalName(), messageQR);
				
				startActivity(actQR);
			}
		});
		
		offer = (Offer) getIntent().getParcelableExtra(Offer.class.getCanonicalName());
		
		punchCard = (PunchCard) getIntent().getParcelableExtra(PunchCard.class.getCanonicalName());
		
		if (punchCard.getBonuses() >= offer.getBonuses()) {
			btnUseOffer.setEnabled(true);
		} else {
			btnUseOffer.setEnabled(false);
		}
		
		TextView offerName = (TextView) findViewById(R.id.offer_name);
		
		offerName.setText(offer.getName());
		
		TextView offerPrice = (TextView) findViewById(R.id.offer_price);
		
		String price = getString(R.string.offer_price);
		
		price = price.replace(getString(R.string.mask_mark_count),
				Integer.toString(offer.getBonuses()));
		
		offerPrice.setText(price);
		
		TextView offerMarkInfo = (TextView) findViewById(R.id.offer_mark_info);
		
		String markInfo = getString(R.string.offer_mark_info);
		
		markInfo = markInfo.replace(getString(R.string.mask_mark_count),
				Integer.toString(punchCard.getBonuses()));
		
		offerMarkInfo.setText(markInfo);

		GridView gridview = (GridView) findViewById(R.id.gridview);
								
		gridview.setAdapter(new ImageAdapter(this));
		
	}
	
	private class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return offer.getBonuses();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			Button textView;
			if (convertView == null) {
				// if it's not recycled, initialize some
				// attributes
				textView = new Button(mContext);
				
				textView.setLayoutParams(new GridView.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			
				textView.setGravity(Gravity.CENTER);
				
				
			
			} else {
				textView = (Button) convertView;
			}
			
			textView.setText("" + (position + 1));
			if (position >= punchCard.getBonuses()) {
				textView.setBackgroundResource(R.drawable.mark_empty);
				textView.setTextColor(getResources().getColor(
						android.R.color.darker_gray));
			
			} else {
				textView.setBackgroundResource(R.drawable.mark);
				textView.setTextColor(getResources().getColor(
						android.R.color.white));
			}
		
			return textView;
		}

	}
}
